﻿using SchoolCommunicationSystem.Models;
using SCS.Business;
using SCS.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SchoolCommunicationSystem.Controllers
{
    public class ParrentsController : Controller
    {
        private ParentServices _parentServices = new ParentServices();
        private MembershipService _memberServices = new MembershipService();

        /// <summary>
        /// Index
        /// </summary>
        /// <returns>View</returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Pre get grade by Subject
        /// </summary>
        /// <returns>View</returns>
        public ActionResult GradesAll()
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            var subjectNameList = _parentServices.GetAllSubject().Select(t => new SelectListItem()
            {
                Text = t.subjectName,
                Value = t.subjectID.ToString(),
            }).ToList();


            ParentAndLearnerModel.learnerName = Learner.firstName + " " + Learner.lastName;
            ParentAndLearnerModel.className = Learner.Class.ClassInSchoolYears.FirstOrDefault().className;
            ParentAndLearnerModel.SubjectSelectList = subjectNameList;
            ParentAndLearnerModel.grade = _parentServices.Grade(Learner.learnerID);

            return View(ParentAndLearnerModel);
        }

        /// <summary>
        /// get grade by Subject
        /// </summary>
        /// <param name="_subjectID">subjectID</param>
        /// <param name="page">1</param>
        /// <param name="sortDir">ASC</param>
        /// <returns>View</returns>
        [HttpPost]
        public ActionResult GradesAll(int _subjectID, int page = 1, string sortDir = "ASC")
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            ParentAndLearnerModel.learnerName = Learner.firstName + " " + Learner.lastName;
            var subjectNameList = _parentServices.GetAllSubject().Select(t => new SelectListItem()
            {
                Text = t.subjectName,
                Value = t.subjectID.ToString(),
            }).ToList();

            const int pageSize = 10;
            var totalRows = _parentServices.CountParentViewGradeBySubject(1, _subjectID);
            if (totalRows <= pageSize)
            {
                page = 1;
            }
            sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";
            IList<Grade> grades = null;

            grades = _parentServices.Grade(Learner.learnerID, _subjectID);

            var data = new ParentAndLearnerModels()
            {
                TotalRows = totalRows,
                PageSize = pageSize,
                learnerName = Learner.firstName + " " + Learner.lastName,
                className = Learner.Class.ClassInSchoolYears.FirstOrDefault().className,
                SubjectSelectList = subjectNameList,
                grade = grades
            };
            return View(data);
        }

        /// <summary>
        /// pre get GradesByDate
        /// </summary>
        /// <returns>View</returns>
        public ActionResult GradesByDate()
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            ParentAndLearnerModel.learnerName = Learner.firstName + " " + Learner.lastName;
            ParentAndLearnerModel.className = Learner.Class.ClassInSchoolYears.FirstOrDefault().className;
            ParentAndLearnerModel.grade = _parentServices.Grade(Learner.learnerID);
            ParentAndLearnerModel.createDate = null;
            ParentAndLearnerModel.firstDate = null;
            ParentAndLearnerModel.lastDate = null;

            return View(ParentAndLearnerModel);
        }

        /// <summary>
        /// get GradesByDate
        /// </summary>
        /// <param name="firstDate">firstDate</param>
        /// <param name="lastDate">lastDate</param>
        /// <param name="page">1</param>
        /// <param name="sortDir">ASC</param>
        /// <returns>View</returns>
        [HttpPost]
        public ActionResult GradesByDate(DateTime firstDate, DateTime lastDate, int page = 1, string sortDir = "ASC")
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            ParentAndLearnerModel.learnerName = Learner.firstName + " " + Learner.lastName;

            const int pageSize = 10;
            var totalRows = _parentServices.CountParentViewGrade(1, firstDate, lastDate);
            if (totalRows <= pageSize)
            {
                page = 1;
            }
            sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";
            IList<Grade> grades = null;
            if (firstDate == null || lastDate == null)
            {
                grades = _parentServices.Grade(Learner.learnerID);
            }
            else
            {
                grades = _parentServices.Grade(Learner.learnerID, page, pageSize, firstDate, lastDate);
            }
            var data = new ParentAndLearnerModels()
            {
                TotalRows = totalRows,
                PageSize = pageSize,
                grade = grades
            };
            return View(data);
        }

        /// <summary>
        /// pre get all comment
        /// </summary>
        /// <returns>View</returns>
        public ActionResult ViewComment()
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();
            //CommentInClass Comment = new CommentInClass();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            ParentAndLearnerModel.learnerName = Learner.firstName + " " + Learner.lastName;
            ParentAndLearnerModel.className = Learner.Class.ClassInSchoolYears.FirstOrDefault().className;
            ParentAndLearnerModel.comment = _parentServices.Comment(Learner.learnerID);
            ParentAndLearnerModel.dateOfComment = null;
            ParentAndLearnerModel.firstDate = null;
            ParentAndLearnerModel.lastDate = null;

            return View(ParentAndLearnerModel);
        }

        /// <summary>
        /// get all comment
        /// </summary>
        /// <param name="firstDate">firstDate</param>
        /// <param name="lastDate">lastDate</param>
        /// <param name="page">1</param>
        /// <param name="sortDir">ASC</param>
        /// <returns>View</returns>
        [HttpPost]
        public ActionResult ViewComment(DateTime firstDate, DateTime lastDate, int page = 1, string sortDir = "ASC")
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();
            //CommentInClass Comment = new CommentInClass();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            ParentAndLearnerModel.learnerName = Learner.firstName + " " + Learner.lastName;

            const int pageSize = 10;
            var totalRows = _parentServices.CountParentViewComment(firstDate, lastDate);
            if (totalRows <= pageSize)
            {
                page = 1;
            }
            sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";
            IList<CommentInClass> comments = null;
            if (firstDate == null || lastDate == null)
            {
                comments = _parentServices.Comment(Learner.learnerID);
            }
            else
            {
                comments = _parentServices.Comment(page, pageSize, firstDate, lastDate, Learner.learnerID);
            }
            var data = new ParentAndLearnerModels()
            {
                TotalRows = totalRows,
                PageSize = pageSize,
                comment = comments
            };
            return View(data);
        }

        /// <summary>
        /// View Teacher Information
        /// </summary>
        /// <param name="page">1</param>
        /// <param name="sortDir">ASC</param>
        /// <returns>View</returns>
        public ActionResult ViewTeacherInformation(int page = 1, string sortDir = "ASC")
        {
            Learner Learner = new Learner();
            ParentAndLearnerModels ParentAndLearnerModel = new ParentAndLearnerModels();

            string userName = User.Identity.Name;
            var parent = _memberServices.GetUser(userName);
            int parentID = parent.Parents.FirstOrDefault().parentID;
            Learner = _parentServices.getLearnerID(parentID);

            IList<TeachingAssignment> teachingAssign = _parentServices.TeachingAssignment(Learner.classID ?? 0, 1);
            //////////////////////////////////
            const int pageSize = 10;
            var totalRows = _parentServices.CountParentViewTeacherInformation();
            if (totalRows <= pageSize)
            {
                page = 1;
            }
            sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";
            var data = new ParentAndLearnerModels()
            {
                learnerName = Learner.firstName + " " + Learner.lastName,
                className = _parentServices.getClassName(Learner.classID ?? 0),
                homeroomTeacher = Learner.Class.Teacher,
                schoolYearName = Learner.Class.ClassInSchoolYears.FirstOrDefault().SchoolYear.schoolYearName,

                TotalRows = totalRows,
                PageSize = pageSize,
                teachingAssignment = teachingAssign
            };
            return View(data);
        }
    }
}
